import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Kiiruste_liitmine extends JPanel implements ActionListener {

	JButton tagasi, liida;
	JPanel ylemine, keskmine, u_panel, v_panel, s_panel, uc_pnl, vc_pnl, sc_pnl;
	JTextArea u_tekst, v_tekst;
	JLabel s_tekst, uc_tekst, vc_tekst, sc_tekst;
	NumberFormat nf = new DecimalFormat("#0.00000");
	double k_u, k_v, rel_kiirus;

	public Kiiruste_liitmine() {
		setLayout(new BorderLayout(3, 3));
		ylemine = new JPanel();
		ylemine.setLayout(new FlowLayout(FlowLayout.LEFT));
		tagasi = new JButton("<< TAGASI");
		tagasi.addActionListener(this);
		ylemine.add(tagasi);
		ylemine.setBorder(BorderFactory.createEtchedBorder());
		add(ylemine, BorderLayout.NORTH);

		keskmine = new JPanel();
		keskmine.setLayout(new GridLayout(5,2));
		
		u_tekst = new JTextArea(1, 10);
		v_tekst = new JTextArea(1, 10);
		s_tekst = new JLabel("299792458");
		u_tekst.setText("299792458");
		v_tekst.setText("299792458");
		u_tekst.setBorder(BorderFactory.createLineBorder(Color.black));
		v_tekst.setBorder(BorderFactory.createLineBorder(Color.black));
		s_tekst.setFont(new Font("Arial", 1, 13));
		uc_tekst = new JLabel("1");
		vc_tekst = new JLabel("1");
		sc_tekst = new JLabel("1");
		
		u_panel = new JPanel();
		u_panel.add(new JLabel("Kiirus u : "));
		u_panel.add(u_tekst);
		u_panel.add(new JLabel(" m/s"));
		keskmine.add(u_panel);
		uc_pnl = new JPanel();
		uc_pnl.setLayout(new FlowLayout(FlowLayout.LEFT));
		uc_pnl.add(new JLabel("Kiirus c suhtes : "));
		uc_pnl.add(uc_tekst);
		uc_pnl.add(new JLabel(" c"));
		keskmine.add(uc_pnl);
		
		v_panel = new JPanel();
		v_panel.add(new JLabel("Kiirus v : "));
		v_panel.add(v_tekst);
		v_panel.add(new JLabel(" m/s"));
		keskmine.add(v_panel);
		vc_pnl = new JPanel();
		vc_pnl.setLayout(new FlowLayout(FlowLayout.LEFT));
		vc_pnl.add(new JLabel("Kiirus c suhtes : "));
		vc_pnl.add(vc_tekst);
		vc_pnl.add(new JLabel(" c"));
		keskmine.add(vc_pnl);
		
		s_panel = new JPanel();
		s_panel.add(new JLabel("Kiiruste relativistlik summa: "));
		s_panel.add(s_tekst);
		s_panel.add(new JLabel(" m/s"));
		keskmine.add(s_panel);
		sc_pnl = new JPanel();
		sc_pnl.setLayout(new FlowLayout(FlowLayout.LEFT));
		sc_pnl.add(new JLabel("Kiirus c suhtes : "));
		sc_pnl.add(sc_tekst);
		sc_pnl.add(new JLabel(" c"));
		keskmine.add(sc_pnl);
		
		liida = new JButton("Answer to Life, the Universe, and Everything !");
		liida.setFont(new Font("Arial", 1, 13));
		liida.addActionListener(this);
		keskmine.add(liida);
		keskmine.add(new JPanel()); // tyhi paneel
		ImageIcon pilt = null;
		try {
			pilt = Ikoonilaadija.annaIkoon("Valemid/Relativistlikud_kiirused.png");
		} catch (Exception e) {
			System.out.println("mingi porr");
		}
		keskmine.add(new JLabel(pilt));
		keskmine.add(new JLabel(" c = 299 792 458 m/s"));
		add(keskmine, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "<< TAGASI") {
			this.setVisible(false);
		}
		if (e.getActionCommand() == liida.getText()) {
			try {
				liida.setText("Answer to Life, the Universe, and Everything !");
				liida.setBackground(null);
				k_u = Double.parseDouble(u_tekst.getText());
				k_v = Double.parseDouble(v_tekst.getText());
				
				if(k_u > 299792458 || k_v > 299792458) {
					liida.setBackground(Color.pink);
					liida.setText("Viga! Sisesta kiirus 0 kuni 299 792 458 m/s.");
					s_tekst.setText("- - -");
				}
				else {
					uc_tekst.setText(nf.format(k_u / 299792458));
					vc_tekst.setText(nf.format(k_v / 299792458));
					rel_kiirus = (k_u + k_v) / (1 + ((k_u * k_v) / Math.pow(299792458, 2)));
					sc_tekst.setText(nf.format(rel_kiirus / 299792458));
					s_tekst.setText(Integer.toString((int) rel_kiirus));
				}
			} catch (Exception evt) {
				liida.setBackground(Color.pink);
				liida.setText("Viga andmete sisestamisel! Kontrolli ja proovi uuesti.");
			}
		}
	}
}
